package com.stdspringboot.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stdspringboot.entity.Goods;
import com.stdspringboot.mapper.IGoodsDAO;
import com.stdspringboot.service.IGoodsService;
import com.stdspringboot.vo.GoodsVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class GoodsSrvImpl implements IGoodsService {

    @Resource
    private IGoodsDAO dao;

    /**
     * 根据商品名称、价格最小值、最大值进行查询
     * @param goodsVO
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo query(GoodsVO goodsVO, int pageNum, int pageSize) {
        //商品名称后面加%，支持模糊查询
        String name=goodsVO.getName();
        if(name!=null) {
            goodsVO.setName(name+"%");
        }
        Page page= PageHelper.startPage(pageNum, pageSize, true);
        List list=dao.query(goodsVO);
        //第一个参数：查询结果
        //第二个参数：总共的页数
        PageInfo pageInfo=new PageInfo(list,page.getPages());

        return pageInfo;
    }

    @Override
    public int deleteById(int id) {
        return dao.deleteById(id);
    }

    @Override
    public int add(Goods goods , List urlList) {
        Date date =new Date();
        //将第一张图片作为默认图片
        String url=(String)urlList.get(0);
        goods.setImg(url);
        goods.setCreateTime(date);
        goods.setModifyTime(date);
        goods.setDel(0);
        int i=dao.insert(goods);

        return i;
    }

    @Override
    public GoodsVO showById(int id) {
        return dao.showById(id);
    }

    @Override
    public int modifyById(Goods goods) {
        Date date=new Date();
        goods.setModifyTime(date);
        return dao.updateByPrimaryKeySelective(goods);
    }
}
